package cj.web.vote.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.vote.dao.SongDAO;
import cj.web.vote.dao.VoteDAO;
import cj.web.vote.domain.Song;
import cj.web.vote.domain.Vote;

/**
 * 音乐管理
 * @author Administrator
 *
 */
@Path("/song")
public class SongController extends BaseController{
	
	@Autowired
	private SongDAO songDao;
	
	@Get("show")
	public String show(Model model) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.tpglyygl);
		return "/views/vote/songList.vm";
	}

	@Post("getList")
	public String queryList(Song song) {
		JSONObject jo = new JSONObject();
		List<Song> arr = songDao.query(song);
		int size = songDao.querysize(song);
		jo.put("data", arr);
		jo.put("draw", song.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
        //MemuEnum
        model.add("MemuEnum",MemuEnum.tpglyygl);
		return "/views/vote/songDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Song song,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			songDao.create(song);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		
		Song song =songDao.queryById(id);
		
        //MemuEnum
        model.add("MemuEnum",MemuEnum.tpglyygl);
        model.add("obj",song);
		return "/views/vote/songEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Song song,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			songDao.update(song);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		songDao.deleteById(id);
		return "r:show";
	}
}